using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using TuyennvLibrary.SqlClient;
namespace TuyennvLibrary.ServerApi.Models
{
    public class SMSProcessClass
    {
        public int id { get; set; }
        public string command_code { get; set; }
        public string subcodes { get; set; }
        public string short_code { get; set; }
        public string class_proccess { get; set; }
        public int status { get; set; }
        public int partner { get; set; }
        public string file_dll { get; set; }
        public string developer { get; set; }
        public DateTime createddate { get; set; }

        public static string COLUMN_ID = "id";
        public static string COLUMN_COMMAND_CODE = "command_code";
        public static string COLUMN_SUBCODES = "subcodes";
        public static string COLUMN_SHORT_CODE = "short_code";
        public static string COLUMN_CLASS_PROCCESS = "class_proccess";
        public static string COLUMN_STATUS = "status";
        public static string COLUMN_PARTNER = "partner";
        public static string COLUMN_FILE_DLL = "file_dll";
        public static string COLUMN_DEVELOPER = "developer";
        public static string COLUMN_CREATEDDATE = "createddate";

        public static string TableName = "SMSProcessClass";
        public SMSProcessClass()
        {
			this.id = 0;
			this.command_code = string.Empty;
			this.subcodes = string.Empty;
			this.short_code = string.Empty;
			this.class_proccess = string.Empty;
			this.status = 0;
			this.partner = 0;
            this.file_dll = string.Empty;
            this.developer = string.Empty;
            this.createddate = DateTime.Now;
        }
    }
    public class SMSProcessClassModel:BaseModel<SMSProcessClass>
    {
        public SMSProcessClassModel()
        {
            table = SMSProcessClass.TableName;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Insert(SMSProcessClass obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter("command_code",obj.command_code),
					new SqlParameter("subcodes",obj.subcodes),
					new SqlParameter("short_code",obj.short_code),
					new SqlParameter("class_proccess",obj.class_proccess),
					new SqlParameter("status",obj.status),
					new SqlParameter("partner",obj.partner),
                    new SqlParameter("file_dll",obj.file_dll),
                    new SqlParameter("developer",obj.developer),
                });
                return true;
                
            
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Update(SMSProcessClass obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("command_code",obj.command_code),
					new SqlParameter("subcodes",obj.subcodes),
					new SqlParameter("short_code",obj.short_code),
					new SqlParameter("class_proccess",obj.class_proccess),
					new SqlParameter("status",obj.status),
					new SqlParameter("partner",obj.partner),
                    new SqlParameter("file_dll",obj.file_dll),
                    new SqlParameter("developer",obj.developer),

                },new SqlParameter[]{
					new SqlParameter("id",obj.id),
																																																		 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Delete(int id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter("id",id),
																																																		 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
		public bool DeleteMulti(string ListID)
        {
            try
            {
				if(ListID!=string.Empty)
				{
					foreach(string ID in ListID.Split(','))
					{
						Delete(int.Parse(ID));
					}
				}
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<SMSProcessClass> GetByPage(int page, int pagesize, List<SMSProcessClass> source)
        {
            List<SMSProcessClass> list = new List<SMSProcessClass>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
		public List<SMSProcessClass> GetListBy(DataTable dt)
		{
			List<SMSProcessClass> list = new List<SMSProcessClass>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        SMSProcessClass obj = GetBy(dr);
                        list.Add(obj);
                    }
                }
                return list;
		}
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);                
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public SMSProcessClass GetBy(DataRow dr)
        {
            try
            {
                SMSProcessClass obj = new SMSProcessClass();
                obj.id = (dr["id"] is DBNull) ? 0 : int.Parse(dr["id"].ToString());
                obj.command_code = (dr["command_code"] is DBNull) ? string.Empty : dr["command_code"].ToString();
                obj.subcodes = (dr["subcodes"] is DBNull) ? string.Empty : dr["subcodes"].ToString();
                obj.short_code = (dr["short_code"] is DBNull) ? string.Empty : dr["short_code"].ToString();
                obj.class_proccess = (dr["class_proccess"] is DBNull) ? string.Empty : dr["class_proccess"].ToString();
                obj.status = (dr["status"] is DBNull) ? 0 : int.Parse(dr["status"].ToString());
                obj.partner = (dr["partner"] is DBNull) ? 0 : int.Parse(dr["partner"].ToString());
                obj.file_dll = (dr["file_dll"] is DBNull) ? string.Empty : dr["file_dll"].ToString();
                obj.developer = (dr["developer"] is DBNull) ? string.Empty : dr["developer"].ToString();
                obj.createddate = (dr["createddate"] is DBNull) ? DateTime.Now : (DateTime)dr["createddate"];
                return obj;
            }
            catch (Exception)
            {
            } 
            return new SMSProcessClass();
        }
		public SMSProcessClass GetBy(DataTable dt)
		{
			if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        return GetBy(dr);
                    }
                }
			return new SMSProcessClass();
		}
        public SMSProcessClass getBy(string command_code, string short_code, string message)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] {
                new SqlParameter("command_code",command_code),
                new SqlParameter("short_code",short_code),
                new SqlParameter("status",1)
            }, "subcodes desc");
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string command = dr["command_code"].ToString().ToUpper();
                        string subcode = dr["subcodes"].ToString().ToUpper();
                        string strCompare = (command + " " + subcode).Trim();
                        
                        if (message.ToUpper().StartsWith(strCompare) || message.ToUpper() == strCompare)
                        {
                            return GetBy(dr);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
            return new SMSProcessClass();
        }
        public DataTable Search(int partner, string serviceid, string command, string developer)
        {
            string query = "select a.*,b.name as partner_name from " + table + " a, partnersms b where a.partner=b.id";
            if (partner != 0)
            {
                query += string.Format(" and a.partner={0}", partner);
            }
            if (serviceid != "0")
            {
                query += string.Format(" and a.short_code ='{0}'", serviceid);
            }
            if (!string.IsNullOrEmpty(command))
            {
                query += string.Format(" and a.command_code like '%{0}%'", command);
            }
            if (!string.IsNullOrEmpty(developer))
            {
                query += string.Format(" and a.developer like '%{0}%'", developer);
            }
            this.Count = GetCountBy(query);
            query = GetQueryPagging(query, "command_code", page, pagesize);
            //query += " order by a.command_code";
            return DB.ExecuteDataTable(query, null);
        }
        public SMSProcessClass GetByID(int id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("id",id),
																																																		
                },null);
                return GetBy(dt);
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public SMSProcessClass GetByPartner(int partner)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("partner",partner),
																																																		
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
